<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/11/9
 * Time: 16:58
 */

namespace app\api\controller;


use think\Db;

class Index extends Common
{
    /**
     * 首页搜索之后的列表
     * page  当前页码
     * cate_id   酒店分类   1 国内    2 国外    3 钟点房
     * sheng 所选择的目的地省份
     * shi   所选择的目的地城市
     * type  1 首页搜索    2 酒店列表搜索
     * hotel_name  模糊搜索的酒店名称   当type为2时传此值
     * @return \think\response\Json
     */
    public function searchHotel(){
        $params = input('param.');
        $auth = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if(!$auth){
            return json(['status'=>301,'msg'=>'Token失效！请重新登录！']);
        }
        $where = [];
        $where['h_cate'] = $params['cate_id'];//酒类分类ID
        $where['h_shi'] = $params['shi'];//所选择的城市ID
        if($params['type'] == 2){
            $where['h_name'] = ['like', "%" . $params['hotel_name'] . "%"];//酒店名称模糊查询
        }
        $page = $params['page']?$params['page']:1;
        //酒店详情
        $hotelLists = Db::table('up_hotel')
            ->alias('uh')
            ->join('up_hotel_type uht', 'uht.type_id = uh.h_type', 'left')
            ->where($where)
            ->field('uh.h_id, uh.h_name, uh.h_grade, uht.type_name, uh.h_address, uh.h_min_price')
            ->page($page, 10)
            ->select();
        return json(['status' => 200, 'data' => $hotelLists]);
    }
    
    /**
     * 获取省市数组
     */
    public function getArea(){
        $shengLists = Db::name('area')->field('district_id, district')->where('level', 1)->select();
        $shiLists = Db::name('area')->field('district_id, district, pid')->where('level', 2)->select();
        foreach($shengLists as $key => $shengList){
            for($i = 0; $i < count($shiLists); $i++){
                if($shiLists[$i]['pid'] == $shengList['district_id']){
                    $shengLists[$key]['shi'][] = $shiLists[$i];
                }
            }
        }
        return json(['status' => 200, 'data' => $shengLists]);
    }
    
    /**
     * 添加收藏/取消收藏操作
     * hotel_id  酒店ID
     * @return \think\response\Json
     */
    public function collectHotel(){
        $params = input('param.');
        $auth = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if(!$auth){
            return json(['status'=>301,'msg'=>'Token失效！请重新登录！']);
        }
        $res = Db::table('up_my_collect')
            ->where('user_id', $params['user_id'])
            ->where('hotel_id', $params['hotel_id'])
            ->find();
        $collect = 0;
        if($res){//已收藏
            $result = Db::table('up_my_collect')->where('collect_id', $res['collect_id'])->delete();
            if($result){
                $collect = 2;//取消收藏
                $msg = '取消收藏成功';
                $status = 200;
            }else{
                $msg = '取消收藏失败';
                $status = 0;
            }
        }else{//未收藏
            $collect = 1;//添加收藏
            $result = Db::table('up_my_collect')
                ->insert([
                    'user_id' => $params['user_id'],//用户ID
                    'hotel_id' => $params['hotel_id'],//酒店ID
                    'create_time' => date('Y-m-d H:i:s')//添加收藏时间
            ]);
            if($result){
                $collect = 1;//添加收藏
                $msg = '添加收藏成功';
                $status = 200;
            }else{
                $msg = '添加收藏失败';
                $status = 0;
            }
        }
        return json(['status' => $status, 'data' => $collect, 'msg' => $msg]);
    }
    
    /**
     * 酒店详情
     * hotel_id 酒店ID
     * @return \think\response\Json
     */
    public function hotelDetail(){
        $params = input('param.');
        $auth = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if(!$auth){
            return json(['status'=>301,'msg'=>'Token失效！请重新登录！']);
        }
        //酒店详情
        $hotelInfo = Db::table('up_hotel')
            ->where('h_id', $params['hotel_id'])
            ->field('h_log, h_grade, h_address, h_wifi, h_wried, h_p, h_phone')
            ->find();
        //该酒店房间类型详情
        $roomTypeInfo = Db::table('up_room_type')
            ->where('hotel_id' , $params['hotel_id'])
            ->field('type_id, type_name, min_price')//是否含早、床型、是否有窗字段未添加
            ->order('min_price asc')
            ->select();
        //该酒店是否已被收藏
        $res = Db::table('up_my_collect')
            ->where('user_id', $params['user_id'])
            ->where('hotel_id', $params['hotel_id'])
            ->find();
        if($res){
            $collect = 1;//已经收藏
        }else{
            $collect = 2;//未收藏
        }
        $hotelInfo['collect'] = $collect;
        $hotelInfo['son'] = $roomTypeInfo;
        return json(['status' => 200, 'data' => $hotelInfo]);
    }
    
    /**
     * 房间类型详情
     * room_type_id  房间类型ID
     * @return \think\response\Json
     */
    public function roomTypeInfo(){
        $params = input('param.');
        $auth = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if(!$auth){
            return json(['status'=>301,'msg'=>'Token失效！请重新登录！']);
        }
        //酒店房间类型详情
        $roomTypeInfo = Db::table('up_room_type')
            ->where('type_id', $params['room_type_id'])
            ->field('type_id, type_log')//有无wifi、卫浴、有无窗、有无早餐、可住人数、床型等字段未添加
            ->find();
        return json(['status' => 200, 'data' => $roomTypeInfo]);
    }
}